﻿using Microsoft.Extensions.DependencyInjection;
using Quartz;

namespace Luka;

/// <summary>
/// QuartzJob
/// </summary>
[NotScanned]
public sealed class QuartzJob : IJob
{
    /// <summary>
    /// 执行定时任务内容
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public Task Execute(IJobExecutionContext context)
    {
        var data = context.JobDetail.JobDataMap.Get("obj") as QuartzData;
        var scope = data.ServiceProvider?.CreateScope();
        var controller = scope.ServiceProvider.GetService(data.ControllerType);
        return controller == null
            ? throw new NotImplementedException($"AttributeQuartz：未正确配置控制器{data.ControllerType.Name}的依赖注入！")
            : Task.Run(() =>
        {
            data.Method?.Invoke(controller, null);
        });
    }
}